import _ from 'lodash'
import Ls from 'store'
import { UPDATE_ALL_AUTHORITY_TYPE, CLEAR_ALL_AUTHORITY_TYPE } from './types'

const initState = {
  adminname: '',
  token: '',
  role: -1,
  checkedkeys: [],
}
export interface IloginInfo {
  adminname?: string,
  password?: string,
}
export interface Iaction {
  type?: string,
  data?: IloginInfo
}
const reducer = (state = Ls.get('logininfo', { ...initState }), action :Iaction = {}) => {
  const newState = _.cloneDeep(state)
  const { type, ...payload } = action
  switch (type) {
    case UPDATE_ALL_AUTHORITY_TYPE:
      Object.assign(newState, payload.data)
      break;
    case CLEAR_ALL_AUTHORITY_TYPE:
      Object.assign(newState, initState)
      break;
    default:
      break;
  }
  return newState
}
export default reducer
